package com.hfut.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;

import com.hfut.dao.GlobalDao;
import com.hfut.model.Book;
import com.hfut.model.RequestBook;
@Component
public class GlobalDaoImpl implements GlobalDao {
	private SessionFactory sessionFactory;
	@Override
	
	public List<Book> queryByPage(String hql, int offset, int pageSize) {
		Session session = sessionFactory.openSession();
        Transaction tx = null;
        List<Book> list = null;
        try {
            tx = session.beginTransaction();
            Query query = session.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize);
            list = query.list();
            tx.commit();
        } catch (Exception e) {
            if(tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return list;
    }

	@Override
	public int getAllRowCount(String hql) {
		 Session session = sessionFactory.openSession();
	        Transaction tx = null;
	        int allRows = 0;
	        try {
	            tx = session.beginTransaction();
	            Query query = session.createQuery(hql);
	            allRows = query.list().size();
	            tx.commit();
	        } catch (Exception e) {
	            if(tx != null) {
	                tx.rollback();
	            }
	            e.printStackTrace();
	        }
	        finally {
	            session.close();
	        }
	        return allRows;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<RequestBook> queryRequest(String hql) {
		 Session session = sessionFactory.openSession();
		 Transaction tx = null;
		 try {
	            tx = session.beginTransaction();
	            Query query = session.createQuery(hql);
	            query.setMaxResults(5);
	            List<RequestBook> list = query.list();
	            tx.commit();
	            return list;
	        } catch (Exception e) {
	            if(tx != null) {
	                tx.rollback();
	            }
	            e.printStackTrace();
	        }
	        finally {
	            session.close();
	        }
		return null;
	}

	@Override
	public List<Book> queryBook(String hql) {
		 Session session = sessionFactory.openSession();
		 Transaction tx = null;
		 try {
	            tx = session.beginTransaction();
	            Query query = session.createQuery(hql);
	            query.setMaxResults(8);
	            List<Book> list = query.list();
	            tx.commit();
	            return list;
	        } catch (Exception e) {
	            if(tx != null) {
	                tx.rollback();
	            }
	            e.printStackTrace();
	        }
	        finally {
	            session.close();
	        }
		return null;
	}

	@Override
	public Book querryBookById(int id) {
		 Session session = sessionFactory.openSession();
		 Transaction tx = null;
		 try {
	            tx = session.beginTransaction();
	            Book b = (Book)session.get(Book.class,id);
	            tx.commit();
	            return b;
	        } catch (Exception e) {
	            if(tx != null) {
	                tx.rollback();
	            }
	            e.printStackTrace();
	        }
	        finally {
	            session.close();
	        }
		return null;
	}

}
